﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;

public partial class student_onlineExam : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //只显示学生选了课的考试
        string test_question_id = "";
        string test_question_name = "";
        //Session["student_id"] = "3120182109222";
        System.Data.DataTable table1 = new System.Data.DataTable();
        int count = 0;//总记录的条数

        try
        {
            //填充数据
            int page = Int32.Parse(Request.QueryString["page"].ToString().Trim());
            int limit = Int32.Parse(Request.QueryString["limit"].ToString().Trim());
            if (Request.QueryString["test_question_id"] != null)
            {
                test_question_id = Request.QueryString["test_question_id"].ToString().Trim();
            }
            if (Request.QueryString["test_question_name"] != null)
            {
                test_question_name = Request.QueryString["test_question_name"].ToString().Trim();
            }
            
            string sqlGetDataCount = "select count(*) from tb_test_questions where course_id in   "
                + "(select course_id from tb_score where student_id = '" + Session["student_id"]+"')";

            List<string> wheres = new List<string>();

            if (test_question_id != "")
            {
                wheres.Add("test_question_id like '%" + test_question_id + "%'");
            }
            if (test_question_name != "")
            {
                wheres.Add("test_question_name like '%" + test_question_name + "%'");
            }
            string wh = "";
            if (wheres.Count > 0)
            {
                wh = string.Join(" and ", wheres.ToArray());
                sqlGetDataCount = sqlGetDataCount + " where " + wh;
            }
            count = Int32.Parse(SqlServer.ExecuteScalar(sqlGetDataCount));
            int page_temp = page;
            int limit_temp = limit;
            if (count < page * limit)
            {
                limit = count - (page - 1) * limit;
            }
            page = (page_temp - 1) * limit_temp;
            string sql = "select test_question_id,test_question_name,course_id,radio_questions_sum,mutil_questions_sum,"
                        +"judge_questions_sum,answer_questions_sum from tb_test_questions where course_id in   "
                + "(select course_id from tb_score where student_id = '" + Session["student_id"] + "')";
            if (wheres.Count > 0)
            {
                sql = sql + " and  " + wh + " and test_question_id not in (select top " + page + "  test_question_id from tb_test_questions " + " where " + wh 
                    + " where course_id in " +"(select course_id from tb_score where student_id = '" + Session["student_id"] + "')"+")";
                
            }
            else
            {
                sql = sql + " and test_question_id not in (select top " + page + "  test_question_id from tb_test_questions "+
                   " where course_id in " +"(select course_id from tb_score where student_id = '" + Session["student_id"] + "')"+ ")";
            }
            table1 = SqlServer.GetDataTable(sql);
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('有错误')</script>");
            Response.End();
        }
        string a = Json.ToJson(table1, count);
        Response.Write(Json.ToJson(table1, count));
        Response.End();

    }


}